;
var role_set_ops = {
    init:function() {
        this.eventBind();
    },
    eventBind:function() {
        // 添加角色
        $('#btn-submit').on('click', function() {
            var id = $('input[name=id]').val();
            var role_name = $('input[name=role_name]').val();

            if (role_name == '') {
                layer.msg("角色名称不能为空");
                return false;
            }

            $.ajax({
                type:"post",
                url:"/admin/role/set",
                data:{id:id, role_name:role_name},
                dataType:"json",
                success:function(data) {
                    if (data.code != 1) {
                        layer.msg(data.msg);
                        return false;
                    }
                    tipsDump("/admin/role/index");
                }
            })

        });

        // 删除角色
        $('.btn-del').on('click', function() {
            var id = $(this).data('id');

            if (id == "") {
                layer.msg("参数错误");
                return false;
            }

            layer.confirm('确定要删除用户吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                // layer.msg('的确很重要', {icon: 1});
                $.ajax({
                    type:"post",
                    url:"/admin/role/del",
                    data:{id:id},
                    dataType:"json",
                    success:function(data) {
                        if (data.code != 1) {
                            layer.msg(data.msg);
                            return false;
                        }
                        tipsDump();
                    }
                })
            }, function(){
                // return false;
            });
        });

        // 添加权限
        $('#btn-submit-access').on('click', function() {
            var role_id = $('input[name=role_id]').val();

            if (role_id == "") {
                layer.msg("参数错误");
                return false;
            }

            var access_ids = [];
            $('input[name="access_ids[]"]').each(function() {
                if( $(this).prop("checked") ){
                    access_ids.push( $(this).val() );
                }
            });

            $.ajax({
                type:"post",
                url:"/admin/role/setting",
                data:{role_id:role_id, access_ids:access_ids},
                dataType:"json",
                success:function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg);
                        return false;
                    }
                    tipsDump("/admin/role/index");
                }
            });

        });
    }
};
$(function() {
    role_set_ops.init();
})